package com.dfish.downloadtool.util;

import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.yuhanxun.util.DealHttp;

public class MyDownloadToolHttp {
	
	//checkAppVer
	public static CheckVersionBean getVersion(String request) {
		String ret = null;
		int count = 0;
		do { // 3次机会，避免未知错误造成失败
			ret = DealHttp.getXmlKeyValue(request);
			count++;
		} while ((ret == null || ret.equals("")) && count < 3);

		if (ret == null)
			return null;
		// 解析错误，删除第一行
		ret = ret.substring(ret.indexOf("\n") + 1, ret.length());

		XmlPullParserFactory pullParserFactory;
		CheckVersionBean item = null;
		try {
			pullParserFactory = XmlPullParserFactory.newInstance();
			XmlPullParser xmlPullParser = pullParserFactory.newPullParser();
			try {
				xmlPullParser.setInput(new StringReader(ret));
			} catch (IllegalStateException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			int eventType = xmlPullParser.getEventType();
			try {
				while (eventType != XmlPullParser.END_DOCUMENT) {
					String nodeName = xmlPullParser.getName();

					switch (eventType) {
					// 文档开始
					case XmlPullParser.START_DOCUMENT:
						break;
					// 开始节点
					case XmlPullParser.START_TAG:
						if ("html".equals(nodeName)) {
							return null;
						} else if ("info".equals(nodeName)) {
							item = new CheckVersionBean();
						} else if ("string".equals(nodeName)) {
							String attribute = xmlPullParser.getAttributeValue(
									null, "name");
							if (attribute != null) {
								if (attribute.equals("filename"))
									item.filename = xmlPullParser.nextText();
								else if (attribute.equals("version"))
									item.version = xmlPullParser.nextText();
								else if (attribute.equals("description"))
									item.description = xmlPullParser.nextText();
								else if (attribute.equals("Apkadd"))
									item.apkAddUrl = xmlPullParser.nextText();
							}
						}
						break;
					// 结束节点
					case XmlPullParser.END_TAG:
						if ("info".equals(nodeName)) {
							return item;
						}
						break;
					default:
						break;
					}
					eventType = xmlPullParser.next();
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
				return null;
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return item;
	}
	
	/**
	 * @param request
	 * @return 请求不到则返回null
	 */
	public static String[] getAddress(String request) {
		String ret = null;
		String[] ret2 = null;
		int count = 0;
		do { // 3次机会，避免未知错误造成失败
			ret = DealHttp.getXmlKeyValue(request);
			count++;
		} while ((ret == null || ret.equals("")) && count < 3);

		
		if(ret!=null && !ret.equals("")){
			
			ret2 = ret.split("\n");
		} else {
			return null;
		}
		if(ret2.length > 0) {
			for(int i=0;i<ret2.length;i++){
				ret2[i] = ret2[i].replaceAll("\\s*", "");
				ret2[i] = ret2[i].replaceAll("\0", "");
			}
		} else {
			return null;
		}
		
		return ret2;
	}
	
}
